package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import java.math.BigDecimal;

public class CNMONEY extends NormalFunction
{
  private String[] straChineseUnit = { "分", "角", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };
  private String[] straChineseNumber = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

  public Object run(Object[] paramArrayOfObject)
  {
    Object localObject1;
    String str1 = "";
    int i = paramArrayOfObject.length;
    if (i == 1)
    {
      localObject1 = paramArrayOfObject[0];
      if (localObject1 instanceof String)
        try
        {
          str1 = MoneyToChinese(new BigDecimal((String)localObject1));
        }
        catch (Exception localException1)
        {
          str1 = Primitive.ERROR_VALUE.toString();
        }
      else if (localObject1 instanceof BigDecimal)
        try
        {
          str1 = MoneyToChinese((BigDecimal)localObject1);
        }
        catch (Exception localException2)
        {
          str1 = Primitive.ERROR_VALUE.toString();
        }
      else if (localObject1 instanceof Number)
        try
        {
          BigDecimal localBigDecimal1 = new BigDecimal(((Number)localObject1).doubleValue());
          str1 = MoneyToChinese(localBigDecimal1);
        }
        catch (Exception localException3)
        {
          str1 = Primitive.ERROR_VALUE.toString();
        }
      else
        str1 = Primitive.ERROR_VALUE.toString();
    }
    else if (i == 2)
    {
      localObject1 = paramArrayOfObject[0];
      Object localObject2 = paramArrayOfObject[1];
      if (localObject1 instanceof Number)
      {
        BigDecimal localBigDecimal2 = new BigDecimal(((Number)localObject1).doubleValue());
        String str2 = localObject2.toString();
        if (str2.equalsIgnoreCase("s"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(10.0D));
        else if (str2.equalsIgnoreCase("b"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(100.0D));
        else if (str2.equalsIgnoreCase("q"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(1000.0D));
        else if (str2.equalsIgnoreCase("w"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(10000.0D));
        else if (str2.equalsIgnoreCase("sw"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(100000.0D));
        else if (str2.equalsIgnoreCase("bw"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(1000000.0D));
        else if (str2.equalsIgnoreCase("qw"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(10000000.0D));
        else if (str2.equalsIgnoreCase("y"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(100000000.0D));
        else if (str2.equalsIgnoreCase("sy"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal(1000000000.0D));
        else if (str2.equalsIgnoreCase("by"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal("10000000000"));
        else if (str2.equalsIgnoreCase("qy"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal("100000000000"));
        else if (str2.equalsIgnoreCase("wy"))
          localBigDecimal2 = localBigDecimal2.multiply(new BigDecimal("1000000000000"));
        try
        {
          str1 = MoneyToChinese(localBigDecimal2);
        }
        catch (Exception localException4)
        {
          str1 = Primitive.ERROR_VALUE.toString();
        }
      }
      else
      {
        str1 = Primitive.ERROR_VALUE.toString();
      }
    }
    else
    {
      str1 = Primitive.ERROR_VALUE.toString();
    }
    return str1;
  }

  private String MoneyToChinese(BigDecimal paramBigDecimal)
    throws Exception
  {
    String str = "";
    int i = 1;
    int j = 0;
    if (paramBigDecimal == null)
      return null;
    if (paramBigDecimal.doubleValue() == 0.0D)
      return "零圆整";
    double d = Math.round(paramBigDecimal.doubleValue() * 100.0D);
    int k = (d < 0.0D) ? 1 : 0;
    d = Math.abs(d);
    while (d > 0.0D)
    {
      if ((j == 2) && (str.length() == 0))
        str = str + "整";
      if (d % 10.0D > 0.0D)
      {
        str = this.straChineseNumber[(int)(d % 10.0D)] + this.straChineseUnit[j] + str;
        i = 0;
      }
      else
      {
        if (j == 2)
        {
          if (d > 0.0D)
          {
            str = this.straChineseUnit[j] + str;
            i = 1;
          }
        }
        else if ((((j == 6) || (j == 10))) && (d % 10000.0D > 0.0D))
        {
          str = this.straChineseUnit[j] + str;
          i = 1;
        }
        if (i == 0)
          str = this.straChineseNumber[0] + str;
        i = 1;
      }
      d = Math.floor(d / 10.0D);
      ++j;
    }
    if (k != 0)
      str = "负" + str;
    return str;
  }

  public Function.Type getType()
  {
    return Function.TEXT;
  }

  public String getCN()
  {
    return "CNMONEY(number,unit)返回人民币大写。\nnumber:需要转换的数值型的数。\nunit:单位，\"s\",\"b\",\"q\",\"w\",\"sw\",\"bw\",\"qw\",\"y\",\"sy\",\"by\",\"qy\",\"wy\"分别代表“拾”，“佰”，“仟”，“万”，“拾万”，“佰万”，“仟万”，“亿”，“拾亿”，“佰亿”，“仟亿”，“万亿”。\n备注:\n    单位可以为空，如果为空，则直接将number转换为人民币大写，否则先将number与单位的进制相乘，然后再将相乘的结果转换为人民币大写。\n示例:\nCNMONEY(1200)等于壹仟贰佰圆整。\nCNMONEY(12.5,\"w\")等于壹拾贰万伍仟圆整。\nCNMONEY(56.3478,\"bw\")等于伍仟陆佰叁拾肆万柒仟捌佰圆整。\nCNMONEY(3.4567,\"y\")等于叁亿肆仟伍佰陆拾柒万圆整。";
  }

  public String getEN()
  {
    return "";
  }
}